//
//  ChatToSaryAlertController.swift
//  btvideo
//
//  Created by mac on 2020/11/9.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyAttributes

class ST_ChatForSryAlertController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        view.backgroundColor = .clear
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.width.equalTo(root_view_width)
            make.height.equalTo(409)
            make.center.equalToSuperview()
        }
        
        root_view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        root_view.addSubview(pageControl)
        pageControl.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(210)
            make.height.equalTo(5+10)
        }
        
        root_view.addSubview(next_btn)
        next_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-37)
            make.height.equalTo(44)
            make.width.equalTo(200)
        }
        
        root_view.addSubview(check_box_btn)
        check_box_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(next_btn.snp.bottom).offset(10)
        }
        
        
        let scroll_content_view = UIView()
        scroll_content_view.backgroundColor = .white
        scrollView.addSubview(scroll_content_view)
        scroll_content_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalTo(root_view_width*4)
            make.height.equalTo(409)
        }
        
        let json = SCComponets.rich_text_storage_component.json["sys_schedule"]
        
        let cell0 = ChatToSaryAlertCell.init(title: json[0]["title"].stringValue, content: json[0]["content"].stringValue, animationName: "ChatToSaryAlert001")
        scroll_content_view.addSubview(cell0)
        cell0.snp.makeConstraints { (make) in
            make.left.top.bottom.equalToSuperview()
            make.width.equalTo(root_view_width)
        }
        cell0.play()
        
        let cell1 = ChatToSaryAlertCell.init(title: json[1]["title"].stringValue, content: json[1]["content"].stringValue, animationName: "ChatToSaryAlert002")
        scroll_content_view.addSubview(cell1)
        cell1.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.left.equalTo(cell0.snp.right)
            make.width.equalTo(root_view_width)
        }
        
        
        
        let cell2 = ChatToSaryAlertCell.init(title: json[2]["title"].stringValue, content: json[2]["content"].stringValue, animationName: "ChatToSaryAlert003")
        scroll_content_view.addSubview(cell2)
        cell2.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.left.equalTo(cell1.snp.right)
            make.width.equalTo(root_view_width)
        }
        
        let cell3 = ChatToSaryAlertCell.init(title: json[3]["title"].stringValue, content: json[3]["content"].stringValue, animationName: "ChatToSaryAlert004")
        scroll_content_view.addSubview(cell3)
        cell3.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.left.equalTo(cell2.snp.right)
            make.width.equalTo(root_view_width)
        }
        
        childCells = [cell0,cell1,cell2,cell3]
        
    }
    
    
//    MARK: - Properties
    fileprivate lazy var root_view_width = 300.cgFloat
    
    fileprivate lazy var childCells = [ChatToSaryAlertCell]()
    
    fileprivate var currentPage = 0{
        didSet{
            
            self.pageControl.currentPage = self.currentPage
            
            childCells[safe: currentPage]?.play()
            
        }
    }
    
    fileprivate lazy var root_view:UIView = {
       let control = UIView()
        control.cornerRadius = 20
        control.backgroundColor = .white
        return control
    }()
    
    fileprivate lazy var scrollView:UIScrollView = {
       let control = UIScrollView()
        control.backgroundColor = .clear
        control.showsHorizontalScrollIndicator = false
        control.isPagingEnabled = true
        control.isScrollEnabled = false
        control.delegate = self
        return control
    }()
    
    fileprivate lazy var pageControl:UIPageControl = {
        let control = UIPageControl()
        control.numberOfPages = 4
        control.currentPage = 0
        control.pageIndicatorTintColor = UIColor.init(hex: 0xE7E7E7)
        control.currentPageIndicatorTintColor = UIColor.init(hex: 0xFEC207)
        return control
    }()
    
    fileprivate lazy var next_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hex: 0xFEC207)
        control.cornerRadius = 44/2.0
        control.setTitleForAllStates("下一步")
        control.setTitleColorForAllStates(UIColor.white)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        control.addTarget(self, action: #selector(next_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var check_box_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        control.setTitleForAllStates(" 允许系统打招呼")
        control.setTitleColor(UIColor.init(hex: 0xACB4C0)!, for: UIControl.State.normal)
        control.setImage(ST_ImageUtils.imageName("checkboxround0")!, for: UIControl.State.normal)
        control.setImage(ST_ImageUtils.imageName("checkboxround1")!, for: UIControl.State.selected)
//        control.semanticContentAttribute = .forceRightToLeft
        control.addTarget(self, action: #selector(check_box_action), for: UIControl.Event.touchUpInside)
        control.isSelected = true
        control.alpha = 0
        return control
    }()
    
//    MARK: Event
    
    @objc fileprivate func check_box_action(){
        
        check_box_btn.isSelected = !check_box_btn.isSelected
        
        
    }
    
    @objc fileprivate func next_handle(){
        
        self.next_btn.isUserInteractionEnabled = false
                
        main_thread_execute(afterDelay: 1) {
            self.next_btn.isUserInteractionEnabled = true
        }
        
        let current = currentPage
        
        if current == 3 {
            
            goback_previous_controller()
            
        }else{
            
            if current + 1 == 3{
                
                next_btn.setTitleForAllStates("立即发消息")
                check_box_btn.alpha = 1
            }
            
            currentPage = current+1
                
            let offSet = CGPoint.init(x: root_view_width * (current+1).cgFloat, y: 0)
            
            scrollView.setContentOffset(offSet, animated: true)
            
        }
        
    }
}

//MARK: - UIScrollViewDelegate
extension ST_ChatForSryAlertController:UIScrollViewDelegate{
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        stoppedScrolling()
    }
    
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        
        print("scrollViewWillEndDragging----scrollViewWillEndDragging")
        
        scrollView.isUserInteractionEnabled = false
        
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        
        if decelerate == false{
            stoppedScrolling()
        }
    }

    func stoppedScrolling(){
        
        
        let integerPart = (scrollView.contentOffset.x/root_view_width).int
        
        if currentPage != integerPart{
   
            currentPage = integerPart
            
        }else{
            print("不交换----")
        }
        
        scrollView.isUserInteractionEnabled = true
        
    }
    
}




//MARK: - child Cell
class ChatToSaryAlertCell: UIControl {
    

    convenience init(title:String,content:String,animationName:String){
        
        self.init(frame:CGRect.zero)
        
        titleLabel.text = title
        
        let paragh = NSParagraphStyle.create_line_spacing_paragraph_style(lineSpacing: 3)
        
        paragh.alignment = .center
        
        content_label.attributedText = content.withAttributes([Attribute.textColor(UIColor.init(hex: 0x61666F)!),Attribute.font(Font.systemFont(ofSize: 14, weight: UIFont.Weight.regular)),Attribute.paragraphStyle(paragh)])
        
        guard  let path = Bundle.main.path(forResource: animationName, ofType: "json") else{
             return
        }
        
        let animationControl = LOTAnimationView.init(filePath: path)

        animationControl.contentMode = UIView.ContentMode.scaleAspectFill
        
        self.addSubview(animationControl)
        animationControl.snp.makeConstraints { (make) in
            make.height.equalTo(200)
            make.left.right.top.equalToSuperview()
        }
        
        self.animationView = animationControl
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.height.equalTo(40)
            make.width.equalTo(300)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-106)
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.width.equalTo(300)
            make.height.equalTo(25)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-154)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    fileprivate var animationView:LOTAnimationView?
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor(hexString: "#333333")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium)
        return control
    }()

    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.numberOfLines = 0
        return control
    }()

    
    func play() {
        animationView?.play()
    }
    
}
